package com.shuai.shuaisecondhand_springboot.controller;


import com.shuai.shuaisecondhand_springboot.common.Constants;
import com.shuai.shuaisecondhand_springboot.common.Result;
import com.shuai.shuaisecondhand_springboot.entity.Collection;
import com.shuai.shuaisecondhand_springboot.exception.ServiceException;
import com.shuai.shuaisecondhand_springboot.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/collection")
public class CollectionControler {
    @Autowired
    CollectionService collectionService;


    //根据用户id查询数据
    @GetMapping("/getCollection")
    public Map<String, Object> findCollection(@RequestParam("Uid") String Uid, @RequestParam("name") String name) {

        List<Collection> data = collectionService.findCollection(Uid, name);
        Integer total = collectionService.getSidTotal(Uid, name);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;

    }

    //根据用户id和商品id查询数据
    @GetMapping("/getMyCollection")
    public Boolean findMyCollection(@RequestParam("Uid") String Uid, @RequestParam("Gid") String Gid) {
        return collectionService.findMyCollection(Uid, Gid);
    }

    //增加
    @PostMapping("/insertCollection")
    public Result addCollection(@RequestParam("Uid") String Uid, @RequestParam("Gid") String Gid) {
        if (collectionService.insertCollection(Uid, Gid)) {
            return Result.success("收藏成功");
        } else {
            throw new ServiceException(Constants.CODE_600, "后台异常");//自定义异常
        }

    }

    //删除
    @DeleteMapping("/deleteCollection/{Uid}&{Gid}")
    public Integer deleteCollection(@PathVariable("Uid") Long Uid, @PathVariable("Gid") Long Gid) {
        return collectionService.deleteCollection(Uid, Gid);
    }
}
